package swa23.provider.impl;
 
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import swa2012.telco.broker.BrokerCallback;
import swa2012.telco.broker.beans.Client;
import swa2012.telco.broker.beans.Message;
//import swa2012.telco.broker.beans.Provider;
import swa2012.telco.broker.beans.ReceiveMessageStatus;
import swa23.provider.interfaces.ClientRegistry;
import swa23.provider.interfaces.MessageRepository;
//import swa2012.telco.sp.ClientRegistry;
//import swa2012.telco.sp.MessageRepository;
 
public class BrokerCallbackImpl implements BrokerCallback {
	private static final Logger LOG = LoggerFactory.getLogger(BrokerCallbackImpl.class);
	private final TelcoProvider provider;
  	private final ClientRegistry clientRegistry;
  	private final MessageRepository messageRepository;
  	
  	public BrokerCallbackImpl(TelcoProvider provider, ClientRegistry clientRegistry, MessageRepository messageRepository) {
  		this.provider = provider;
  		this.clientRegistry = clientRegistry;
  		this.messageRepository = messageRepository;
  		LOG.info(this + " started up BrokerCallback: " + this);
	}
 
	public ReceiveMessageStatus receiveMessage(Message msg) {
		String sender = msg.getSender().getID() + "@" + msg.getSender().getProvider().getID();
		String receiver = msg.getReceiver().getID() + "@" + msg.getReceiver().getProvider().getID();
		LOG.info(this.provider + " Received Message: " + sender + " -> " + receiver);
		this.messageRepository.addMessage(msg);
		return ReceiveMessageStatus.RECEIVE_MESSAGE_OK;
	}
 
   public Collection<Client> listClients() {
	   LOG.info(this.provider + " listClients...");
	   return this.clientRegistry.getClients();
   }
 
   public boolean ping() {
	   LOG.info(this.provider + " Ping!");
	   return true;
   }
 
   public String toString() {
	   StringBuilder sb = new StringBuilder();
	   sb.append(getClass().getSimpleName()).append("[provider=").append(this.provider).append("]");
	   return sb.toString();
   }
 }